package vn.intelsoft.vlogvn.view;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import vn.intelsoft.vlogvn.MainActivity;
import vn.intelsoft.vlogvn.R;
import vn.intelsoft.vlogvn.adapter.TabAdapter;
import vn.intelsoft.vlogvn.instance.Banner;
import vn.intelsoft.vlogvn.instance.Instance;
import vn.intelsoft.vlogvn.instance.Log;
import vn.intelsoft.vlogvn.instance.Vlogger;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class Slider extends AbsCustomView implements OnPageChangeListener {

	Context mContext;
	int idImage;
	private ViewPager viewPager;
	private CirclePageIndicator mIndicator;
	private TabAdapter mAdapter;
	private List<Vlogger> vloggers;
	private TimerTask mTimerTask;
	private Timer mTimer = new Timer();
	private final Handler handler = new Handler();
	private int position = 0;

	public Slider(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
		setContentView(R.layout.slider);
		init();
	}

	public Slider(Context context, List<Vlogger> vloggers) {
		// TODO Auto-generated constructor stub
		super(context);
		this.mContext = context;
		this.vloggers = new ArrayList<Vlogger>();
		this.vloggers = vloggers;
		setContentView(R.layout.slider);
		init();
	}

	private void init() {
		LinearLayout layout_slider = (LinearLayout) this
				.findViewById(R.id.layout_slider);
		LayoutParams layoutParams = (LayoutParams) layout_slider
				.getLayoutParams();
		layoutParams.height = Instance.height(mContext) / 3;
		layout_slider.setLayoutParams(layoutParams);

		viewPager = (ViewPager) this.findViewById(R.id.pager);

		List<View> views = new ArrayList<View>();

		Collections.shuffle(vloggers);

		for (int i = 0; i < 5; i++) {
			Pager pager = new Pager(mContext, vloggers.get(i));
			views.add(pager);
		}

		mAdapter = new TabAdapter(mContext, views);
		viewPager.setAdapter(mAdapter);

		mIndicator = (CirclePageIndicator) findViewById(R.id.page_indicator);
		mIndicator.setViewPager(viewPager);
		mIndicator.setOnPageChangeListener(this);

		this.viewPager.setOnTouchListener(new View.OnTouchListener() {
			public boolean onTouch(View paramAnonymousView,
					MotionEvent paramAnonymousMotionEvent) {

				Slider.this.viewPager.getParent()
						.requestDisallowInterceptTouchEvent(true);

				return false;
			}
		});

		doTimerTask();

	}

	private void doTimerTask() {
		this.mTimerTask = new TimerTask() {
			@Override
			public void run() {
				Slider.this.handler.post(new Runnable() {
					@Override
					public void run() {
						if (position == 5) {
							position = 0;
							// viewPager.setCurrentItem(position);
							mIndicator.setCurrentItem(position);

						} else {
							// viewPager.setCurrentItem(position);
							mIndicator.setCurrentItem(position);
							position++;
						}
					}
				});
			}
		};

		this.mTimer.schedule(this.mTimerTask, 0L, 2500L);
	}

	public void stopTask() {
		if (this.mTimerTask != null) {
			this.mTimerTask.cancel();
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		this.position = position;
		if (position == 0) {
			((MainActivity) mContext).getSlidingMenu().setTouchModeAbove(
					SlidingMenu.TOUCHMODE_FULLSCREEN);
		} else {
			((MainActivity) mContext).getSlidingMenu().setTouchModeAbove(
					SlidingMenu.TOUCHMODE_MARGIN);
		}
	}

}
